<div class="panel-body" style="padding-bottom:0px;">
	<div class="panel panel-default">
	    <div class="panel-heading">图书信息</div>
	    <div class="panel-body">
	        <form id="searchForm" name="searchForm" class="form-horizontal" action="/demo/book/select">
	              <div class="row" >
                        <label class="control-label col-sm-1" >图书名</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" name="bookName"  >
                        </div>
                        <label class="control-label col-sm-1" >isbn</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" name="isbn" >
                        </div>
                         <label class="control-label col-sm-1">价格</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" name="priceMin">
                        </div>
                         <div class="col-sm-2">
                            <input type="text" class="form-control" name="priceMax">
                        </div>
                    </div>
                    
                    
                    <div class="row" style="margin-top:10px" >
                     	 <div class="col-sm-5"></div>
						<div class="col-sm-7" style="text-align:left;">
                            <button type="button" style="margin-left:50px" id="queryBtn" class="btn btn-primary">查询</button>
                        </div>
                     </div>
	        </form>
	    </div>
	</div>       
	
	
	<div id="toolbar" class="btn-group">
		<button id="btnAdd" type="button" class="btn btn-default">
		    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
		</button>
		<button id="btnEdit" type="button" class="btn btn-default">
		    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
		</button>
		<button id="btnDelete" type="button" class="btn btn-default">
		    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
		</button>
	</div>
	
	
	<table id="queryResult"></table>
</div>

<script>
var tableDiv='queryResult';
var searchForm='searchForm';

$(function(){
	
	var columns=[
		{checkbox: true},
		{field:"bookName",title:"书名"},
		{field:"isbn",title:"ISBN"},
		{field:"price",title:"价格"},
		{field:"createTime",title:"创建时间"},
	]
	
	var queryBtn="queryBtn";
	tableUtil.init(tableDiv,columns,'/demo/book/select','bookId');
	tableUtil.initQuery(queryBtn,tableDiv,searchForm);
		$('#btnAdd').click(function(){
			dialog.form('prepareInsert','新增图书','/demo/book/prepareInsert',function(data){
				var fm=$("#prepareInsert")[0];
				if(!validate.valiForm(fm)){
					return false;
				}
				var data=$("#prepareInsert").serialize();
				ajaxUtil.json('/demo/book/insert',data,function(rest){
					if(rest.success){
						tableUtil.insertRow(rest.data);
					}else{
						dialog.alert(rest.message);
					}
				});
			});
	});
})

$("#btnEdit").click(function(){
    var selects = $('#'+tableDiv).bootstrapTable('getSelections');
    if(selects.length==0){
        dialog.alert("请选择要修改的数据");
        return;
    }
    if(selects.length!=1){
        dialog.alert("每次只能修改1条");
        return;
    }
    var url='/demo/book/prepareUpdate?bookId='+selects[0].bookId;
    dialog.form('prepareUpdate','修改图书',url,function(data){
			var fm=$("#prepareUpdate")[0];
			if(!validate.valiForm(fm)){
				return false;
			}
			var data=$("#prepareUpdate").serialize();
			data+="&bookId="+selects[0].bookId;
			ajaxUtil.json('/demo/book/updateById',data,function(rest){
				if(rest.success){
					tableUtil.update(rest.data.bookId,rest.data);
				}else{
					dialog.alert(rest.message);
				}
			});
		});
  
});

$("#btnDelete").click(function(){
    var selects = $('#'+tableDiv).bootstrapTable('getSelections');
    if(selects.length==0){
        dialog.alert("请选择要删除的数据");
        return;
    }
    var count=selects.length;
    dialog.confirm('确定要删除这'+count+"条数据吗?",function(){
    	var data='';
    	var ids=[];
    	for(var i=0;i<selects.length;i++){
    		data+="bookId="+selects[i].bookId+"&";
    		ids.push(selects[i].bookId);
    	}
    	ajaxUtil.json('/demo/book/deleteById',data,function(resp){
    		tableUtil.remove('bookId',ids);
    	})
    });
});


</script>